{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "import os\n",
    "import imageio\n",
    "from matplotlib import pyplot as plt\n",
    "from math import sqrt\n",
    "import re\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "main_folder = 'Data\\\\sample3\\\\'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### original data stored in folder named 'Original' inside the main_folder\n",
    "\n",
    "when needed, used the following function to create new folder inside the main_folder\n",
    "\n",
    "#### folder created in this script:\n",
    "pad: store processed image data with ideal size and oritentation\n",
    "\n",
    "crop: store cropped images\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ensure_dir(file_path): # create a folder if the folder is not already exist\n",
    "    directory = os.path.dirname(file_path)\n",
    "    if not os.path.exists(directory):\n",
    "        os.makedirs(directory)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Set the required size to large images and small images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_size = 1024 # pad images to 1024 x 1024\n",
    "N_pieces = 16 # crop each images to 16 patches"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_images_from_folder(folder):\n",
    "    images = []\n",
    "    for filename in os.listdir(folder):\n",
    "        f = os.path.join(folder,filename)\n",
    "        img =cv2.imread(f, cv2.IMREAD_GRAYSCALE)\n",
    "        if img is not None:\n",
    "            images.append(img)\n",
    "    return images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data = load_images_from_folder(main_folder+'Original') # \n",
    "print ('original size:', np.shape(data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Clean data to ideal shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. Change the long axis to the main direction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_2 = np.swapaxes(data,1, 2) # change the long axis to the main direction\n",
    "print ('change axis to size:', np.shape(data_2))\n",
    "plt.imshow(data_2[:,:,20], cmap = 'gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. Clean edges that is corrupted "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_2 = np.delete(data_2, np.s_[0:20:1], 0)\n",
    "data_2 = np.delete(data_2, np.s_[-20::1], 0)\n",
    "plt.imshow(data_2[:,:,20], cmap = 'gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. Pad zero to the ideal size, making it easier to crop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_border(data, target_size, path):\n",
    "    # calculate the padding size \n",
    "    top = int((target_size-np.shape(data)[0])/2) \n",
    "    bottom = top\n",
    "    left = int((target_size-np.shape(data)[1])/2)\n",
    "    right = left\n",
    "    \n",
    "    for i in range(0, np.shape(data)[2]):\n",
    "        image = cv2.copyMakeBorder(data[:,:,i], top, bottom, left, right, borderType = 0)\n",
    "        imageio.imwrite(path+\"\\\\\"+str(i)+'.jpg', image)   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ensure_dir(main_folder+'pad'+'\\\\') # create a folder called pad to store image that with border\n",
    "make_border(data_2, target_size, main_folder+'pad')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. Crop image to small pieces"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_crop_write(read_path, write_path, N_pieces): \n",
    "    # read images from folders and cropped them to N_pieces\n",
    "    #images = []\n",
    "    for filename in os.listdir(read_path):\n",
    "        f = os.path.join(read_path,filename)\n",
    "        img =cv2.imread(f, cv2.IMREAD_GRAYSCALE)\n",
    " \n",
    "        I_crop_v  =  np.asarray(np.split(img, sqrt(N_pieces), axis = 0)) \n",
    "        # when split in this axis can be recovered by using I_crop_recover = I_crop_v.reshape(1024,1024)\n",
    "        I_crop_vv = np.asarray(np.split(I_crop_v, sqrt(N_pieces), axis = 2))\n",
    "        \n",
    "        for i in range(0,int(sqrt(N_pieces))):\n",
    "            for j in range (0,int(sqrt(N_pieces))):\n",
    "                prefix= re.sub(\"\\D\", \"\", filename)\n",
    "                filename_new = write_path + '\\\\'+ prefix + '_'+ str(i)+'_'+str(j) +'.jpg'\n",
    "                cv2.imwrite(filename_new, I_crop_vv[j, i, :, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ensure_dir(main_folder+'crop'+'\\\\') \n",
    "# create a folder called pad to store image that with border\n",
    "\n",
    "read_crop_write(main_folder+'pad', main_folder+'crop', N_pieces)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
